package com.zjbti.supermarket;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;

    private String[] names={"蛋糕","面包","鸡蛋","牛奶"};
    private String[] prices={"蛋糕","面包","鸡蛋","牛奶"};
    private String[] shops={"蛋糕","面包","鸡蛋","牛奶"};

    private int[] pictures={R.drawable.daily_nuts,R.drawable.dough_twist,R.drawable.meat_floss,R.drawable.salt_baked_eggs};

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        setContentView(R.layout.activity_main);

        mListView=findViewById(R.id.lv_list);

        MyBaseAdapter mAdapter = new MyBaseAdapter(this);

        mListView.setAdapter(mAdapter);
    }
    class MyBaseAdapter extends BaseAdapter{
        private Context mContext;
        public MyBaseAdapter(Context context){
            this.mContext=context;
        }
        /**
         * 获取列表条目的总数
         */
        @Override
        public int getCount(){
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View converView, ViewGroup parent) {
            converView= LayoutInflater.from(mContext).inflate(R.layout.list_item,null);

            TextView tv_name=converView.findViewById(R.id.tv_name);
            TextView tv_price=converView.findViewById(R.id.tv_price);
            TextView tv_shop=converView.findViewById(R.id.tv_shop);
            ImageView iv_img=converView.findViewById(R.id.iv_img);
            tv_name.setText(names[position]);
            tv_price.setText(prices[position]);
            tv_shop.setText(shops[position]);
            iv_img.setBackgroundResource(pictures[position]);

            return converView;
        }
    }
}